﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DrawLineCtrl : MonoBehaviour
{
    public float Tiling = 0.2f;

    private readonly Vector3 hideModelPos = new Vector3(5000, 0, 0);

    [SerializeField] private GameObject _line;

    private CurveMeshBuilder curveMeshBuilder;

    private Vector3 startPos;

    List<Vector2> listPos = new List<Vector2>();

    void Awake()
    {
        startPos = _line.transform.localPosition;
        _line.transform.localPosition = new Vector3(5000, 0, 0);
        curveMeshBuilder = _line.GetComponent<CurveMeshBuilder>();
        curveMeshBuilder.InitLine();
    }

    public void SetNodeList(List<Vector2> listPos, float scale, double distance)
    {
        //_line.SetActive(false);
        curveMeshBuilder.nodeList = listPos;
        curveMeshBuilder.uvTiling = (float)distance * Tiling;
        curveMeshBuilder.SetLine();
        _line.transform.localPosition = startPos;
    }

    public void ResetNodeList()
    {
        _line.transform.localPosition = hideModelPos;
    }

    private void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            listPos.Add((Input.mousePosition));
        }
        else if (Input.GetMouseButtonUp(0))
        {
            SetNodeList(listPos,1,1);
        }
        if (Input.GetMouseButtonDown(1))
        {
            listPos = new List<Vector2>();
            curveMeshBuilder.InitLine();
        }
    }
}
